package cn.iocoder.yudao.module.trade.api.store;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.api.store.dto.StoreReqDTO;
import cn.iocoder.yudao.module.trade.api.store.dto.StoreRespDTO;
import cn.iocoder.yudao.module.trade.convert.delivery.DeliveryPickUpStoreConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryPickUpStoreDO;
import cn.iocoder.yudao.module.trade.dal.mysql.delivery.DeliveryPickUpStoreMapper;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
@Validated
public class StoreApiImpl implements StoreApi {

    @Resource
    private DeliveryPickUpStoreMapper deliveryPickUpStoreMapper;

    @Override
    public List<StoreRespDTO> getStoreList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Lists.newArrayList();
        }
        List<DeliveryPickUpStoreDO> deliveryPickUpStoreDOS = deliveryPickUpStoreMapper.selectBatchIds(ids);
        return DeliveryPickUpStoreConvert.INSTANCE.convertListToStoreRespDTO(deliveryPickUpStoreDOS);
    }

    @Override
    public List<StoreRespDTO> getEnableStoreList() {
        List<DeliveryPickUpStoreDO> deliveryPickUpStoreDOS = deliveryPickUpStoreMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return DeliveryPickUpStoreConvert.INSTANCE.convertListToStoreRespDTO(deliveryPickUpStoreDOS);
    }

    /**
     * 根据id查询店铺信息
     *
     * @param id
     * @return
     */
    @Override
    public StoreRespDTO getStore(Long id) {
        DeliveryPickUpStoreDO deliveryPickUpStoreDO = deliveryPickUpStoreMapper.selectById(id);
        return DeliveryPickUpStoreConvert.INSTANCE.convert1(deliveryPickUpStoreDO);
    }

    @Override
    public List<StoreRespDTO> storeListApi(StoreReqDTO reqDTO) {
        List<DeliveryPickUpStoreDO> storeList = deliveryPickUpStoreMapper.selectList(new LambdaQueryWrapperX<DeliveryPickUpStoreDO>()
                .likeIfPresent(DeliveryPickUpStoreDO::getName, reqDTO.getName())
                .eqIfPresent(DeliveryPickUpStoreDO::getPhone, reqDTO.getPhone())
                .eqIfPresent(DeliveryPickUpStoreDO::getStatus, reqDTO.getStatus())
                .orderByDesc(DeliveryPickUpStoreDO::getId));
        return BeanUtils.toBean(storeList, StoreRespDTO.class);
    }


}
